package com.ljf.interestingmaps.maps;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ljf.interestingmaps.streetview.MapandBean;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class AboutActivity extends WebView {

    private Context context;
    private int requestCode = 0;
    private String city = "";
    private boolean isOpenGPS = false;
    private boolean isOpenWifi = false;
    private boolean isLoadMap = false;
    private boolean hasCurrentLocation = false;
    private Timer timer;
    private String lat = "";
    private String lang = "";

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            doHandler(msg);
        }
    };

    private void doHandler(Message msg) {
        System.out.println("----------doHandler-----------");
    }

    public AboutActivity(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public AboutActivity(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public AboutActivity(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        getSettings().setJavaScriptEnabled(true);
        setFocusableInTouchMode(true);
        addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
        setWebViewClient(new MyWebViewClient());
    }

    private void doFinished(WebView view, String url) {
        System.out.println("----------doFinished-----------");
    }

    private void currentLocation() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getCurrentLocation();
            }
        }).start();
    }

    private void getCurrentLocation() {
        System.out.println("--------------runTh--------------");
    }

    private void openGps() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                checkGPS();
            }
        }).start();
    }

    private void checkGPS() {
        System.out.println("--------------checkGPS--------------");
    }

    private void openWifi() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                checkWifi();
            }
        }).start();
    }

    private void checkWifi() {
        System.out.println("--------------checkWifi--------------");
    }

    private void refresh() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                doRefresh();
            }
        }, 5 * 1000, 5 * 1000);
    }

    private void doRefresh(){
        System.out.println("----------doRefresh--------");
    }

    final class InJavaScriptLocalObj {
        @JavascriptInterface
        public void showAlert(String type, String username, String nickname, String headimg, String age, String sex, String brith) {
            System.out.println("----------showSource Nice----------");
        }
        @JavascriptInterface
        public String getAlert(String type, String username, String nickname) {
            Log.v("AboutActivity", "getAlert");
            return "test";
        }
    }

    final class MyWebViewClient extends WebViewClient {
        @Nullable
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            System.out.println("----------shouldInterceptRequest----------");
            return super.shouldInterceptRequest(view, request);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            hasCurrentLocation = false;
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            hasCurrentLocation = false;
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            doFinished(view, url);
            super.onPageFinished(view, url);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        if (timer != null)
            timer.cancel();
    }


    private String panoid = "";
    private String isFife = "";
    private MapandBean bean = new MapandBean();
    private ArrayList<String> locations = new ArrayList<>();
}

